package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.entity.AddressBook;
import com.reggie.entity.OrderDetail;
import com.reggie.entity.Orders;
import com.reggie.entity.ShoppingCart;
import com.reggie.mapper.OrdersMapper;
import com.reggie.service.AddressBookService;
import com.reggie.service.OrderDetailService;
import com.reggie.service.OrderService;
import com.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @program: reggie
 * @author:
 * @create: 2022-10-06 17:15
 **/
@Service
@Transactional
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrderService {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;
    /**
     * 订单信息的提交
     * 表：orders与orderDetail(订单表与订单明细)
     * orders一个订单一条数据
     * orderDetail表中，一个订单多条数据
     * */
    @Override
    public void submit(Orders orders, Long userId) {

        //订单号
        Long ordersId = IdWorker.getId();

        //获取购物车中的数据
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);

        //商品金额的累加(原子操作，不会因为多线程计算失误)
        AtomicInteger atomicInteger = new AtomicInteger(0);

        List<OrderDetail> orderDetails = list.stream().map((shoppingCart) -> {
            /**存储orderDetail中的数据*/
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(ordersId);
            orderDetail.setName(orderDetail.getName());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            //单个金额的价格
            orderDetail.setAmount(shoppingCart.getAmount());
            orderDetail.setNumber(shoppingCart.getNumber());
            //总金额的计算 number*amount
            atomicInteger.addAndGet(shoppingCart.getAmount().multiply( new BigDecimal(shoppingCart.getNumber())).intValue());
            return orderDetail;
        }).collect(Collectors.toList());


        //获取地址数据
        //获取地址信息中的用户名称和电话和地址
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        /**设置orders的信息*/
        orders.setAddress(addressBook.getProvinceName()==null? "":addressBook.getProvinceName()+
                addressBook.getCityName()==null? "":addressBook.getCityName()+
                addressBook.getDistrictName()==null? "": addressBook.getDistrictName()+
                addressBook.getDetail()==null ? "":addressBook.getDetail());
        orders.setPhone(addressBook.getPhone());
        //设置收货人的名称
        orders.setConsignee(addressBook.getConsignee());
        //设置下单的时间
        orders.setOrderTime(LocalDateTime.now());
        //设置结账的时间
        orders.setCheckoutTime(LocalDateTime.now());
        //修改订单的信息为带派送状态
        orders.setStatus(2);
        //结账的金额
        orders.setAmount(new BigDecimal(atomicInteger.get()));

        this.save(orders);


        orderDetailService.saveBatch(orderDetails);
        //清空购物车中的数据
        shoppingCartService.remove(wrapper);
    }
}
